package com.carrot.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.carrot.common.core.domain.entity.SysPointEquipment;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.mybatis.BaseServiceImpl;
import com.carrot.system.mapper.SysPointEquipmentMapper;
import com.carrot.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 场站 业务层处理
 *
 * @author liuhongzhen
 */
@Service
public class SysPointEquipmentServiceImpl extends BaseServiceImpl<SysPointEquipmentMapper, SysPointEquipment>
        implements ISysPointEquipmentService {

    @Autowired
    private ISysPointService pointService;

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void addPointEquipment(String stationId, List<SysPointEquipment> pointEquipments) {

        UpdateWrapper<SysPointEquipment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("station_id", stationId);
        //先删除
        this.remove(updateWrapper);
        //批量保存
        this.saveBatch(pointEquipments);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void updatePointEquipment(SysPointEquipment pointEquipment) {

        SysPointEquipment pointEquipment1 = this.query()
                .eq("id", pointEquipment.getId()).one();
        if (null == pointEquipment1) {
            throw new ServiceException("点表设备信息不存在");
        }

        //更新了equipmentId，同步更新点表中的设备id
        UpdateWrapper<SysPointEquipment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("station_id", pointEquipment1.getStationId());
        updateWrapper.eq("equipment_type", pointEquipment1.getEquipmentType());
        updateWrapper.eq("equipment_id", pointEquipment1.getEquipmentId());
        updateWrapper.set("equipment_id", pointEquipment.getEquipmentId());

        this.update(updateWrapper);

        //同步更新
        pointService.batchUpdateEquipmentId(pointEquipment1.getStationId(), pointEquipment1.getEquipmentType(),
                pointEquipment1.getEquipmentId(), pointEquipment.getEquipmentId());
    }


    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void add(SysPointEquipment pointEquipment) {
        this.save(pointEquipment);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void update(SysPointEquipment pointEquipment) {
        SysPointEquipment equipment = this.getById(pointEquipment.getId());
        if (null == equipment) {
            throw new ServiceException("设备信息不存在");
        }
        this.updateById(pointEquipment);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void del(Long id) {
        SysPointEquipment pointEquipment = this.query()
                .eq("id", id).one();
        if (null == pointEquipment) {
            throw new ServiceException("点表设备信息不存在");
        }

        this.removeById(id);
        //同步更新
        pointService.batchDelEquipmentId(pointEquipment.getStationId(), pointEquipment.getEquipmentType(),
                pointEquipment.getEquipmentId());

    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void batchDel(List<Long> ids) {

        List<SysPointEquipment> pointEquipmentList = this.query()
                .in("id", ids).list();
        if (CollUtil.isEmpty(pointEquipmentList)) {
            throw new ServiceException("点表设备信息不存在");
        }

        this.removeBatchByIds(ids);
        for (SysPointEquipment equipment : pointEquipmentList) {
            //同步更新
            pointService.batchDelEquipmentId(equipment.getStationId(), equipment.getEquipmentType(),
                    equipment.getEquipmentId());
        }

    }
}
